package com.bobo.controller;

import com.bobo.model.SelectionSortData;
import com.bobo.view.AlgoFrame;
import com.bobo.units.AlgoVisUnit;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * @Author: bobobo
 * @Date: 2019/9/3 11:34
 * @Version：1.0
 */
public class AlgoVisualizer {
	private static int DELAY = 10;
    // 创建自己的数据
    private SelectionSortData data;  //数据
    private AlgoFrame frame;   //视图
    private boolean isAnimated = true;

    public AlgoVisualizer(int sceneWidth,int sceneHeight,int N){
        //初始化自己的数据
        //初始化数据
        data = new SelectionSortData(N,sceneHeight);

        //初始化视图
        // 事件队列 创建新线程===事件分发线程
        EventQueue.invokeLater(() -> {
            frame = new AlgoFrame("快速排序模拟",sceneWidth,sceneHeight);

            new Thread(() -> {
                run();
            }).start();
        });
    }

    //动画逻辑
    private void run(){

        setData(0,-1,-1);

        // 编写自己的动画逻辑
        for (int i = 0;i < data.N();i++){
            //在 [i,N) 区间最小值索引
            int minIndex = i;
            setData(i,-1,minIndex);

            for (int j = i + 1;j < data.N();j++){
                setData(i,j,minIndex);

                if (data.get(j) < data.get(minIndex)) {
                    minIndex = j;
                    setData(i, j, minIndex);
                }
            }
            data.swap(i,minIndex);

            setData(i+1,-1,-1);
        }

        setData(data.N(),-1,-1);
    }

    private void setData(int orderedIndex,int currentCompareindex,int currentMinindex){

        data.orderedIndex = orderedIndex;
        data.currentCompareIndex = currentCompareindex;
        data.currentMinIndex = currentMinindex;

        frame.render(data);
        AlgoVisUnit.pause(DELAY);
    }

}
